package tools

import (
	"bytes"
	"fmt"
)

func SubString(data string, len int) string {
	conIdx := 0
	ci := 0
	for i, _ := range data {
		if conIdx == 17 {
			ci = i
		}
		if conIdx > len {
			data = data[0:ci]
			break
		}
		conIdx++
	}
	return data
}

// DelEscape 过滤转义符 \f
func DelEscape(str string) string {
	var buf bytes.Buffer
	for _, c := range str {
		if c == '\f' {
			continue
		}
		buf.WriteRune(c)
	}

	return buf.String()
}

// FormatMobileStar 手机号加*号
func FormatMobileStar(mobile string) string {
	if len(mobile) <= 10 {
		return "***********"
	}
	return mobile[:3] + "****" + mobile[7:]
}

// FormatTextStar 文本加*号
func FormatTextStar(content string) string {
	temp := ""
	firstWord := ""
	index := 0
	for i, _ := range content {

		switch index {
		case 0:
			temp = content[0:]
			firstWord = temp
			break
		case 1:
			temp = content[:i]
			firstWord = temp
			break
		default:
			temp += "*"

		}
		index++
	}
	if index <= 1 {
		return fmt.Sprintf("%s*", firstWord)
	}
	return temp
}
